﻿#include "SnapImgItem.h"
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QApplication>
#include <QDebug>

namespace OfUI {

	SnapImgItem::SnapImgItem(QWidget *parent)
		: QWidget(parent)
	{
		m_bInit = false;

		m_pSnapImgViewPointer = nullptr;

		m_pImgWidget = nullptr;
		m_pPathWidget = nullptr;

		m_bSelect = false;
		m_bItemShowBottom = true;

		m_stNormalTopSize = QSize(140, 100);
		m_nBottomHeight = 30;
	}

	SnapImgItem::~SnapImgItem()
	{
	}

	void SnapImgItem::initForm()
	{
		if (!m_bInit)
		{
			m_pImgWidget = initImgWidget();
			m_pPathWidget = initPathWidget();

			// 根据创建的大小作为默认大小, 自定义Item可自行设置顶部/底部区域默认大小
			m_stNormalTopSize = m_pImgWidget->size();

			m_pPathWidget->setFixedSize(m_stNormalTopSize.width(), m_nBottomHeight);
			this->setFixedSize(m_stNormalTopSize + QSize(0, m_bItemShowBottom ? m_nBottomHeight : 0));

			this->setAttribute(Qt::WA_StyledBackground);
			this->installEventFilter(this);

			QVBoxLayout* pMainLayout = new QVBoxLayout(this);
			pMainLayout->setMargin(0);
			pMainLayout->setSpacing(0);
			pMainLayout->addWidget(m_pImgWidget, 1);
			pMainLayout->addWidget(m_pPathWidget);

			refreshStyle();
			m_bInit = true;
		}
	}

	void SnapImgItem::setData(const SnapData& data, bool bRefresh)
	{
		m_stData = data;
		if (bRefresh)
		{
			refreshData();
		}
	}

	SnapData SnapImgItem::getData()
	{
		return m_stData;
	}

	void SnapImgItem::setSelect(bool bSelect)
	{
		if (bSelect != m_bSelect)
		{
			m_bSelect = bSelect;
			refreshStyle();
		}
	}

	bool SnapImgItem::isSelected() const
	{
		return m_bSelect;
	}

	void SnapImgItem::setItemShowBottom(bool bShow)
	{
		if (bShow != m_bItemShowBottom)
		{
			m_bItemShowBottom = bShow;
			m_pPathWidget->setVisible(m_bItemShowBottom);

			QSize dstItemSize = m_pImgWidget->size() + QSize(0, m_bItemShowBottom ? m_pPathWidget->height() : 0);
			this->setFixedSize(dstItemSize);
		}
	}

	void SnapImgItem::setZoom(float fRatio)
	{
		// 底部区域高度固定不变
		QSize dstTopSize = m_stNormalTopSize * fRatio;
		QSize dstBottomSize = QSize(dstTopSize.width(), m_nBottomHeight);
		QSize dstItemSize = dstTopSize + QSize(0, m_bItemShowBottom ? dstBottomSize.height() : 0);

		m_pImgWidget->setFixedSize(dstTopSize);
		m_pPathWidget->setFixedSize(dstBottomSize);
		this->setFixedSize(dstItemSize);
	}

	void SnapImgItem::setGroupIndex(int nIndex)
	{
		m_nRowIndex = nIndex;
	}

	void SnapImgItem::setRowIndex(int nIndex)
	{
		m_nRowIndex = nIndex;
	}

	void SnapImgItem::setColIndex(int nIndex)
	{
		m_nColIndex = nIndex;
	}

	QWidget* SnapImgItem::getSnapViewImgPointer()
	{
		return m_pSnapImgViewPointer;
	}

	void SnapImgItem::setSnapImgViewPointer(QWidget* pWidget)
	{
		m_pSnapImgViewPointer = pWidget;
	}

	void SnapImgItem::resizeEvent(QResizeEvent *event)
	{
		Q_UNUSED(event);
		refreshData();
	}

	void SnapImgItem::mousePressEvent(QMouseEvent *event)
	{
		if (Qt::LeftButton == event->button())
		{
			bool bSingleSelect = true;
			if (Qt::ControlModifier == QApplication::keyboardModifiers())
			{
				bSingleSelect = false;
				this->setSelect(!m_bSelect);
			}
			else
			{
				this->setSelect(true);
			}
			emit signalItemSelectChanged(m_stData, m_bSelect, bSingleSelect);
		}
	}

	void SnapImgItem::contextMenuEvent(QContextMenuEvent *event)
	{
		emit signalItemContextMenu(m_stData, event->globalPos());
	}

	QWidget* SnapImgItem::initImgWidget()
	{
		QLabel* pImgLabel = new QLabel;
		pImgLabel->setObjectName("SnapImgItem_ImgLabel");
		pImgLabel->setFixedSize(m_stNormalTopSize);
		return pImgLabel;
	}

	QWidget* SnapImgItem::initPathWidget()
	{
		QLabel* pLabel = new QLabel;
		pLabel->setObjectName("SnapImgItem_PathLabel");
		return pLabel;
	}

	void SnapImgItem::refreshData()
	{
		// 刷新顶部图片
		if (!m_stData.m_thumbnailImg.isNull())
		{
			QLabel* pImgLabel = findChild<QLabel*>("SnapImgItem_ImgLabel");
			if (pImgLabel)
			{
				QPixmap pix = QPixmap::fromImage(m_stData.m_thumbnailImg);
				pImgLabel->setPixmap(pix.scaled(pImgLabel->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
			}
		}

		// 刷新底部文本
		if (!m_stData.m_strPath.isEmpty())
		{
			QLabel* pTextLabel = findChild<QLabel*>("SnapImgItem_PathLabel");
			if (pTextLabel)
			{
				QString strShowText, strTipText;
				QFontMetrics fm(pTextLabel->font());

				int nStrLen = fm.width(m_stData.m_strPath);
				int nLabLen = pTextLabel->width();

				if (nStrLen > nLabLen)
				{
					strShowText = fm.elidedText(m_stData.m_strPath, Qt::ElideRight, nLabLen);
					strTipText = m_stData.m_strPath;
				}
				else
				{
					strShowText = m_stData.m_strPath;
					strTipText.clear();
				}

				pTextLabel->setText(strShowText);
				this->setToolTip(strTipText);
			}
		}
	}

	void SnapImgItem::refreshStyle()
	{
		QLabel* pImgLabel = findChild<QLabel*>("SnapImgItem_ImgLabel");
		if (pImgLabel)
		{
			if (m_bSelect)
			{
				pImgLabel->setStyleSheet("QLabel{border:2px solid rgb(36,83,164);}");
			}
			else
			{
				pImgLabel->setStyleSheet("QLabel{border:2px solid transparent;}");
			}
		}
		QLabel* pPathLabel = findChild<QLabel*>("SnapImgItem_PathLabel");
		if (pPathLabel)
		{
			if (m_bSelect)
			{
				pPathLabel->setStyleSheet("QLabel{font: 12px \"Arial\"; color:#2453A4;}");
			}
			else
			{
				pPathLabel->setStyleSheet("QLabel{font: 12px \"Arial\"; color:#333333;}");
			}
		}
	}

}
